/**
 * 58. Length of Last Word
 * https://leetcode.com/problems/length-of-last-word/
 */
package lc58;

public class Main {
  public static void main(String[] args) {
    String s = "Hello Wor ld";
    int i = new Solution().lengthOfLastWord(s);
    System.out.println(i);
  }
}

class Solution {
  public int lengthOfLastWord(String s) {
    int n = s.length();
    if (n == 0) return 0;
    int length = 0;
    char[] cs = s.toCharArray();
    while (--n >= 0) {
      if (cs[n] != ' ') {
        length++;
      } else {
        if (length != 0)
          break;
      }
    }
    return length;
  }
}
